package model

import (
	"errors"
	"ginblog/api/common/encrypt"
	"ginblog/utils/errmsg"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type Blog struct {
	BaseModel
	UserName           string `gorm:"type:varchar(20);not null;comment:'名称';uniqueIndex" json:"user_name"`
	PassWord           string `gorm:"type:varchar(32);not null;comment:'密码'" json:"pass_word"`
	Email              string `gorm:"type:varchar(32);not null;comment:'邮箱'" json:"email"`
	Phone              string `gorm:"type:varchar(11);comment:'手机号'" json:"phone"`
	UserIcon           string `gorm:"type:text(512);comment:'用户图标' " json:"user_icon"`
	FriendChain        string `gorm:"type:varchar(32);default 0;comment:'友链'" json:"friend_chain"`
	FriendChainExamine int    `gorm:"type:int(3);default 0;comment:'审核结果,0:未审核,1:审核通过,2:审核拒绝'" json:"friend_chain_examine"`
}

func (B *Blog) FormTokenGetId(c *gin.Context) {
	a, ok := c.Get("blog_id")
	if !ok {
		B.SteCodeMsg(errmsg.ERROR_TOKEN_EXIST)
		return
	}
	B.ID = a.(uint)
}

func (B *Blog) GetUserName() string {
	return B.UserName
}

func (B *Blog) GetUserId() uint {
	return B.ID
}

func (B *Blog) ExaminePassWord(Password string) bool {
	return B.PassWord == Password
}

func (B *Blog) IsUserNameRepeat() bool {
	blog := Blog{}
	result := Db.Where(Blog{UserName: B.UserName}).Find(&blog)
	if result.RowsAffected > 0 && B.ID != blog.ID{
		return true
	}
	return false
}

func (B *Blog) IsPhoneRepeat() bool {
	blog := Blog{}
	result := Db.Where(Blog{Phone: B.Phone}).Find(&blog)
	if result.RowsAffected > 0 && B.ID != blog.ID{
		return true
	}
	return false
}

func (B *Blog) IsEmailRepeat() bool {
	blog := Blog{}
	result := Db.Where(Blog{Email: B.Email}).Find(&blog)
	if result.RowsAffected > 0 && B.ID != blog.ID{
		return true
	}
	return false
}

func (B *Blog) UpdateEmail() bool {
	var maps = make(map[string]interface{})
	maps["email"] = B.Email
	err := Db.Model(&B).Where("id  = ? ", B.ID).Updates(maps).Error
	if err != nil {
		return false
	}
	return true
}

func (B *Blog) UpdatePhone() bool {
	var maps = make(map[string]interface{})
	maps["phone"] = B.Phone
	err := Db.Model(&B).Where("id  = ? ", B.ID).Updates(maps).Error
	if err != nil {
		return false
	}
	return true
}

func (B *Blog) UpdatePassWord() bool {
	var maps = make(map[string]interface{})
	maps["password"] = B.PassWord
	err := Db.Model(&B).Where("id  = ? ", B.ID).Updates(maps).Error
	if err != nil {
		return false
	}
	return true
}

func (B *Blog) UpdateInfo() bool {
	err := Db.Model(&B).Where("id  = ? ", B.ID).Omit("Id", "PassWord").Updates(B).Error
	if err != nil {
		return false
	}
	return true
}

func (B *Blog) PassWordEncrypt(PassWord string) string {
	return encrypt.UseScrypt(PassWord)
}

func (B *Blog) PhoneOREmailRegister() bool {
	err := Db.Create(&B).Error
	if err != nil {
		return false
	}
	return true
}

func (B *Blog) BeforeCreate(tx *gorm.DB) (err error) {
	if B.Phone != "" && B.IsPhoneRepeat() {
		err = errors.New(errmsg.GetErrMsg(errmsg.REGISTER_PHONE_IS_USE))
		B.SteCodeMsg(errmsg.REGISTER_PHONE_IS_USE)
	}
	if B.Email != "" && B.IsEmailRepeat() {
		err = errors.New(errmsg.GetErrMsg(errmsg.REGISTER_EMAIL_IS_USE))
		B.SteCodeMsg(errmsg.REGISTER_EMAIL_IS_USE)
	}
	if B.UserName != "" && B.IsUserNameRepeat() {
		err = errors.New(errmsg.GetErrMsg(errmsg.REGISTER_USERNAME_IS_USE))
		B.SteCodeMsg(errmsg.REGISTER_USERNAME_IS_USE)
	}
	return
}

func (B *Blog) BeforeSave(tx *gorm.DB) (err error) {
	if B.Phone != "" && B.IsPhoneRepeat() {
		err = errors.New(errmsg.GetErrMsg(errmsg.REGISTER_PHONE_IS_USE))
		B.SteCodeMsg(errmsg.REGISTER_PHONE_IS_USE)
	}
	if B.Email != "" && B.IsEmailRepeat() {
		err = errors.New(errmsg.GetErrMsg(errmsg.REGISTER_EMAIL_IS_USE))
		B.SteCodeMsg(errmsg.REGISTER_EMAIL_IS_USE)
	}
	if B.UserName != "" && B.IsUserNameRepeat() {
		err = errors.New(errmsg.GetErrMsg(errmsg.REGISTER_USERNAME_IS_USE))
		B.SteCodeMsg(errmsg.REGISTER_USERNAME_IS_USE)
	}
	return
}
